package com.tanhua.server.controller;
import com.tanhua.domain.db.User;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
/**
 * 用户控制层
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;


    @PostMapping("/saveUser")
    public ResponseEntity add(@RequestBody User user){
        // 添加用户
        userService.add(user);
        // ok方法里带参数，响应数据给客户端，响应体有数据
        // 参数为null, 响应体就没有数据
        return ResponseEntity.ok(null);
    }

    @GetMapping("/findByMobile")
    public ResponseEntity findByMobile(String phone){
        User user = userService.findByMobile(phone);
        // 返回user->json对象
        return ResponseEntity.ok(user);
    }

    /**
     * 登陆发送验证码
     */
    @PostMapping("/login")
    public ResponseEntity sendValidateCode(@RequestBody Map<String,String> paramMap){
        userService.sendValidateCode(paramMap);
        return ResponseEntity.ok(null);
    }

    /**
     * 登入注册，验证码校验
     * @param paraMap
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> paraMap){
        Map<String,Object> resultMap=userService.loginVerification(paraMap);
        return ResponseEntity.ok(resultMap);
    }

}